package com.xhsj.user.hr.process.tbLearningGarden.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xhsj.user.hr.process.dto.tbLearningGarden.LearningStatisticsListDto;
import com.xhsj.user.hr.process.tbLearningGarden.entity.TbLearningGardenContentApply;
import com.xhsj.user.hr.process.tbLearningGarden.query.LearnedListQuery;
import com.xhsj.user.hr.process.tbLearningGarden.query.LearningStatisticsListQuery;
import com.xhsj.user.utils.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.xhsj.user.hr.process.tbLearningGarden.vo.FindToDayStudyNumberVo;
import com.xhsj.user.security.entity.LoginUser;
import com.xhsj.user.hr.system.service.TbGlobalUserService;
import com.xhsj.user.hr.system.vo.GetUserLoginInfoVo;
import com.xhsj.user.utils.UserUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.*;

import com.xhsj.user.utils.Message;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;


import com.xhsj.user.hr.process.tbLearningGarden.service.TbLearningGardenContentLearningRecordService;
import com.xhsj.user.hr.process.tbLearningGarden.entity.TbLearningGardenContentLearningRecord;

import javax.validation.Valid;


/**
 * <p>
 * 学习园地内容申请-学习记录 前端控制器
 * </p>
 *
 * @author 苏小林
 * @since Nov 24, 2020 9:55:43 AM
 */
@Slf4j
@Api(tags = "学习园地内容申请-学习记录")
@RestController
@RequestMapping("/tbLearningGarden/tbLearningGardenContentLearningRecord")
public class TbLearningGardenContentLearningRecordController {

    @Autowired
    public TbLearningGardenContentLearningRecordService service;

    @Autowired
    public TbGlobalUserService tbGlobalUserService;


    /**
     * 保存/修改
     *
     * @author 苏小林
     * @date Nov 24, 2020 9:55:43 AM
     */
    @ApiOperation(value = "学习园地内容申请-学习记录,保存/修改", notes = "学习园地内容申请-学习记录,保存和修改方法")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
//    @PreAuthorize("hasAuthority('tb:learning:garden:content:learning:record:edit')")
    public Message save(@RequestBody TbLearningGardenContentLearningRecord data) {
        try {
            if (StringUtils.isBlank(data.getApplyId())) {
                log.error("applyId不能为空");
                return Message.error(1, "参数有误");
            }
            LoginUser loginAppUser = UserUtils.getLoginAppUser();
            GetUserLoginInfoVo userLoginInfo = tbGlobalUserService.getUserLoginInfo(loginAppUser.getUserId());
            data.setDepartmentId(userLoginInfo.getDeptId());
            data.setDepartmentName(userLoginInfo.getDeptName());
            data.setCreateName(userLoginInfo.getFullName());
            service.save(data);
            return Message.success("成功");
        } catch (Exception e) {
            log.error("保存失败:{}", e);
            return Message.error(1, "失败");
        }
    }


    /**
     * 查询各部门今日学习人数
     *
     * @author 苏小林
     * @date 2020/11/24 14:23
     */
    @RequestMapping(value = "/findToDayStudyNumber", method = RequestMethod.GET)
    public Message findToDayStudyNumber(@RequestParam(value = "month", required = false) String month) {
        try {
            List<FindToDayStudyNumberVo> list = service.findToDayStudyNumber(month);
            for (FindToDayStudyNumberVo findToDayStudyNumberVo : list) {
                findToDayStudyNumberVo.setName(findToDayStudyNumberVo.getName() + " " + findToDayStudyNumberVo.getValue());
            }

            return Message.success("成功", list);
        } catch (Exception e) {
            log.error("保存失败:{}", e);
            return Message.error(1, e.getMessage());
        }
    }


    /**
     * 部门发布信息统计
     *
     * @author 苏小林
     * @date 2020/11/24 17:46
     * @param: month
     * @return: com.xhsj.user.utils.Message
     */
    @RequestMapping(value = "/findDeptPublishInfo", method = RequestMethod.GET)
    public Message findDeptPublishInfo(@RequestParam(value = "month", required = false) String month) {
        try {

            List<FindToDayStudyNumberVo> list = service.findDeptPublishInfo(month);
            return Message.success("成功", list);
        } catch (Exception e) {
            log.error("保存失败:{}", e);
            return Message.error(1, e.getMessage());
        }
    }

    /**
     * 通过id删除数据
     *
     * @author 苏小林
     * @date Nov 24, 2020 9:55:43 AM
     */
    @ApiOperation(value = "学习园地内容申请-学习记录删除", notes = "通过id删除学习园地内容申请-学习记录信息。")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('tb:learning:garden:content:learning:record:delete')")
    public Message delete(@RequestParam(value = "id") Long id) {
        try {
            service.removeById(id);
            return Message.success("成功");
        } catch (Exception e) {
            log.error("删除失败:{}", e);
            return Message.error(1, "失败");
        }

    }

    /**
     * 批量删除
     *
     * @author 苏小林
     * @date Nov 24, 2020 9:55:43 AM
     */
    @ApiOperation(value = "批量删除", notes = "批量删除。")
    @RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:learning:garden:content:learning:record:delete')")
    public Message batchDelete(@RequestBody List<Long> list) {
        try {
            service.removeByIds(list);
            return Message.success("成功");
        } catch (Exception e) {
            log.error("删除失败:{}", e);
            return Message.error(1, "失败");
        }
    }

    /**
     * 分页查询数据
     *
     * @author 苏小林
     * @date Nov 24, 2020 9:55:43 AM
     */
    @ApiOperation(value = "分页查询学习园地内容申请-学习记录list", notes = "带参数查询,{\"page\": 1,\"size\": 5,......}。支持模糊查询")
    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:learning:garden:content:learning:record:view')")
    public Message findList(@RequestBody Map<String, Object> data) {
        try {
            IPage<TbLearningGardenContentLearningRecord> selectPage = service.selectPage(data);
            return Message.success("成功", selectPage.getRecords(), selectPage.getTotal(), selectPage.getPages());
        } catch (Exception e) {
            log.error("查询异常:{}", e);
            return Message.error(1, "失败");
        }
    }

    /**
     * 查询所有数据
     *
     * @author 苏小林
     * @date Nov 24, 2020 9:55:43 AM
     */
    @ApiOperation(value = "查询学习园地内容申请-学习记录所有数据list", notes = "查询学习园地内容申请-学习记录所有数据list。")
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('tb:learning:garden:content:learning:record:view')")
    public Message findAll() {
        return Message.success("成功", service.selectAll());
    }

    /**
     * 通过id获取实体类
     *
     * @author 苏小林
     * @date Nov 24, 2020 9:55:43 AM
     */
    @ApiOperation(value = "通过id获取学习园地内容申请-学习记录实体类", notes = "通过id获取学习园地内容申请-学习记录实体类。")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public Message getById(@RequestParam(value = "id") Long id) {
        return Message.success("成功", service.getById(id));
    }

    /**
     * 通过id查询详细信息
     *
     * @author 苏小林
     * @date Nov 24, 2020 9:55:43 AM
     */
    @ApiOperation(value = "通过id获取实体类详细信息", notes = "通过id获取实体类详细信息。")
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Message detail(@RequestParam(value = "id") Long id) {
        return Message.success("成功", service.getById(id));
    }

    /**
     * @Description: 学习统计按部门列表
     * @Author: liuqinghua
     * @Date: 2020/11/24
     */
    @ApiOperation(value = "学习统计按部门列表", notes = "学习统计按部门列表")
    @PostMapping(value = "/learningStatisticsList")
    public Message learningStatisticsList(@RequestBody @Valid LearningStatisticsListDto data, BindingResult bindingResult) {
        log.info("学习统计按部门列表参数{}", data);
        if (bindingResult.hasErrors()) {
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return Message.error(1, bindingResult.getFieldError().getDefaultMessage());
        }
        IPage<LearningStatisticsListQuery> learningStatisticsList = service.learningStatisticsList(data);
        return Message.success("成功", learningStatisticsList.getRecords(), learningStatisticsList.getTotal(), learningStatisticsList.getPages());
    }

    /**
     * @Description: 查看已学习的名单
     * @Author: liuqinghua
     * @Date: 2020/11/24
     */
    @ApiOperation(value = "查看已学习的名单列表", notes = "查看已学习的名单列表")
    @PostMapping(value = "/learnedList")
    public Message learnedList(@RequestBody @Valid LearningStatisticsListDto data, BindingResult bindingResult) {
        log.info("查看已学习的名单列表参数{}", data);
        if (bindingResult.hasErrors()) {
            log.info(bindingResult.getFieldError().getDefaultMessage());
            return Message.error(1, bindingResult.getFieldError().getDefaultMessage());
        }
        IPage<LearnedListQuery> learnedList = service.learnedList(data);
        return Message.success("成功", learnedList.getRecords(), learnedList.getTotal(), learnedList.getPages());
    }

      /**
          * @Description: 学习园地首页数据统计
          * @Author: liuqinghua
          * @Date: 2020/11/25
          */
    @ApiOperation(value = "学习园地首页数据统计", notes = "学习园地首页数据统计")
    @GetMapping(value="/learningGardenDataStatistics")
    public Message learningGardenDataStatistics(){
        Set<TbLearningGardenContentApply> learningGardenDataStatistics = service.learningGardenDataStatistics();
        return Message.success("成功",learningGardenDataStatistics,learningGardenDataStatistics.size());
    }

}

